<?php

class Courselist extends AppModel {
	var $name= 'Courselist';

//ACL this is a crontrolled object ACO
	var $actsAs = array('Acl' => array('type' => 'controlled'));
	
	function parentNode() {
		if (!$this->id && empty($this->data)) {
			return null;
		}
		if (isset($this->data['Courselist']['application_id'])) {
			$appId = $this->data['Courselist']['application_id'];
		} else {
			$appId = $this->field('application_id');
		}
		if (!$appId) {
			return null;
		} else {
			return array('Application' => array('id' => $appId));
		}
	}
	
	var $validate = array(
		'cgpa_fresh' => array(
			'rule' => '/^[0-4]\.{0,1}[0-9]{0,2}$/i',
			'message' => 'Must be a decimal number of the form "x.xx" between 0 and 4.99'
		),
		'cgpa_soph' => array(
			'rule' => '/^[0-4]\.{0,1}[0-9]{0,2}$/i',
			'message' => 'Must be a decimal number of the form "x.xx" between 0 and 4.99'
		),
		'cgpa_junior' => array(
			'rule' => '/^[0-4]\.{0,1}[0-9]{0,2}$/i',
			'message' => 'Must be a decimal number of the form "x.xx" between 0 and 4.99'
		),
		'cgpa_senior' => array(
			'rule' => '/^[0-4]\.{0,1}[0-9]{0,2}$/i',
			'message' => 'Must be a decimal number of the form "x.xx" between 0 and 4.99'
		),
		'cgpa_all' => array(
			'rule' => '/^[0-4]\.{0,1}[0-9]{0,2}$/i',
			'message' => 'Must be a decimal number of the form "x.xx" between 0 and 4.99'
		),
		'cgpa_grad' => array(
			'rule' => '/^[0-4]\.{0,1}[0-9]{0,2}$/i',
			'message' => 'Must be a decimal number of the form "x.xx" between 0 and 4.99'
		)
	);

/**
* START ASSOCIATION INFORMATION
******************************************************************************
*/
 	
var $belongsTo = array('Application' =>
 												array('className' => 'Application',
															'conditions' => '',
															'order'=> '',
															'foreignKey' => 'application_id'
															)
												); //end belongsTo array.

var $hasMany = array( 'Course'=>
 												array('className' => 'Course',
															'foreignKey' => 'courselist_id',
															)
								 ); //end hasMany array.




/**
* START MODEL FUNCTIONS
******************************************************************************
*/

function check_status($id = null) {
	if ($id)
		$this->id = $id;
			
	$fields = array ('cgpa_fresh','cgpa_soph','cgpa_junior','cgpa_senior','cgpa_all','cgpa_grad');
	$ret = false;
	foreach ($fields as $field) {
		if ($this->field($field) !== null) {
			$ret = true;
		}
	}
	return $ret;
}

function updateStatus() {
	$app_id = $this->field('application_id');
	//update the status of this item;
	$stat = 0;
	If ($this->check_status()) {
		$stat++;
	}
	if ($this->Course->find('count', array(
	                          'conditions' => array('Course.courselist_id' => $this->id))) > 0) {
		$stat++;
	}
	$this->Application->change_status($app_id, 'courses', $stat, '');
}

function afterSave($created = null) {
	$this->updateStatus();
}

}
?>
